<template>
	<view>
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30">
			<u-form :model="model" ref="uForm" style="width: 100%;">
				<u-form-item label-width="150" label="名称:">
					<u-input v-model="model.address" type="text" />
				</u-form-item>
				<u-form-item v-for="(item,index) in checkboxList1" :key="index" label-width="150" :label="item.name" >
					<u-switch  v-model="item.open" @change="change"></u-switch>
				</u-form-item>
			</u-form>
		</view>
		<u-gap height="20"></u-gap>
		<view class="btn-line">
			<button class="landing" type="primary" style="width: 90%;" @click="bindConfirm">{{btnTxt}}</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				equipShow: false,
				visible: false,
				equipList: [],
				model: {
					priority: 1
				},
				type: [],
				detailData: null,
				btnTxt: '保存',
				checkboxList1: [{
						name: '进水开关',
						disabled: false,
						open:false
					},
					{
						name: '出水开关',
						disabled: false,
						open:false
					},
					{
						name: '烟雾报警',
						disabled: false,
						open:false
					},
					{
						name: '空气能主机',
						disabled: false,
						open:false
					},
					{
						name: '自动',
						disabled: false,
						open:false
					}
				],
			};
		},
		onLoad(option) {
			this.detailData = option
			this.model.occurrence = this.$u.timeFrom();
			this.request({
				url: 'BaseEquipment/pages',
				data: {
					limit: 0
				},
				success: res => {
					this.type = res.data;
					this.equipList = res.data;
				}
			});
		},
		onShow() {
			this.initDetail();
		},
		methods: {
			initDetail() {
				console.log('==', this.detailData)
				this.btnTxt = this.detailData.from == 'edit' ? '保存' : '提交'
				uni.setNavigationBarTitle({
					title: this.detailData.from == 'edit' ? this.detailData.name : '新增型号'
				});
			},
			change(n) {
				console.log('change', n,this.checkboxList1);
			},
			confirm(e) {
				if (e.length > 0) {
					this.model.equipId = e[0].value;
				}
			},
			onUploaded(lists, name) {
				this.model.urls = [];
				lists.forEach(m => {
					this.model.urls.push(m.response.data.id);
				});
			},
			onRemove(index, lists, name) {
				if (this.model.urls != undefined && this.model.urls.length > 0) {
					this.model.urls.forEach(m => {
						if (!lists.some(n => n.response.data.id == m)) {
							this.request({
								url: 'files/delete?id=' + m,
								method: 'POST',
								success: res => {}
							});
						}
					})
				}
				this.model.urls = [];
				lists.forEach(m => {
					this.model.urls.push(m.response.data.id);
				});
			},
			getTyp(id) {
				let typ = this.type.find(m => m.code == id);
				if (typ != undefined) {
					return typ.name;
				} else {
					return '请选择设备';
				}
			},
			dateChange(e) {
				this.model.occurrence = e.result;
			},
			scanCode() {
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: res => {
						this.model.equipId = res.result;
					}
				});
			},
			bindConfirm() {
				if (this.model.equipId == undefined || this.model.equipId == '') {
					this.$u.toast('请选择设备');
					return;
				}
				if (this.model.trouble == undefined || this.model.trouble == '') {
					this.$u.toast('请输入问题描述');
					return;
				}
				this.model.status = 1
				if (this.model.urls != undefined) {
					this.model.files = this.model.urls.join(',')
				}
				this.request({
					url: 'EamOrder/add',
					method: 'POST',
					data: this.model,
					success: res => {
						if (res.code == 200) {
							if (res.data == '1') {
								uni.navigateBack();
							}
						} else {
							this.$u.toast(res.msg);
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #FF978D, #FFBB69);
	}
</style>
